package com.shawn.model.vo;

import com.shawn.commons.utils.RC;
import com.shawn.model.entity.UserInfo;
import com.shawn.model.mongo.Movement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MovementsVo implements Serializable {
    private String id;  //动态id
    private Integer userId;
    private String avatar;
    private String nickname;
    private String gender;
    private Integer age;
    private String[] tags;
    private String textContent;
    private String[] imageContent;
    private String distance;
    private String createDate;//动态发布时间
    private Integer likeCount;//点赞数
    private Integer commentCount;//评论数
    private Integer loveCount;//喜欢数
    private Integer hasLiked = 0;//本人是否已点赞 (1已点赞,0未点赞)
    private Integer hasLoved = 0;//本人是否已喜欢  (1已喜欢,0未喜欢)

    public static MovementsVo init(UserInfo userInfo, Movement movement) {
        MovementsVo vo = new MovementsVo();
        //设置动态数据
        BeanUtils.copyProperties(movement, vo);
        vo.setId(movement.getId().toHexString());
        //设置用户数据
        BeanUtils.copyProperties(userInfo, vo);
        //初始化标签
        if (!StringUtils.isEmpty(userInfo.getTags())) {
            vo.setTags(userInfo.getTags().split(","));
        }
        //图片列表
        vo.setImageContent(movement.getMedias().toArray(new String[]{}));
        //设置动态发布时间
        Date date = new Date(movement.getCreated());
        //如果动态发布时间与当前时间相差为一个小时以内,转化为分钟前
        long minute = (System.currentTimeMillis()-date.getTime())/1000/60;
        long hour = minute/60;
        long day = hour / 24;
        if (minute<60){
//            log.info("已设置动态时间为:{}",minute);
            vo.setCreateDate(minute+"分钟前");
        }else if (hour<24){
//            log.info("已设置动态时间为:{}",hour);
            vo.setCreateDate(hour+"小时前");
        }else if (day<3){
            vo.setCreateDate(day+"天前");
        } else {
            vo.setCreateDate(new SimpleDateFormat(RC.GENERAL_DATE_FORMAT).format(date));
        }
        return vo;
    }
}
